package test.model.table;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import test.model.ContactTableModel;

public class ButtonRenderer extends JPanel implements TableCellRenderer{

	private JButton btnButton;
	private boolean initialized = false;
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		if(!initialized){
			btnButton = new JButton("Delete");
			final JTable tb = table;
			final int idx = row;
			btnButton.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					((ContactTableModel)(tb.getModel())).removeItem(idx);
				}
			});
			FlowLayout fll = new FlowLayout();
			fll.setAlignment(FlowLayout.CENTER);
			setLayout(fll);
			add(btnButton);
			initialized = true;
		}
		
		return this;
	}

}
